package com.sys.web.async;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.service.UserService;
import com.sys.service.impl.UserServiceImpl;

/**
 * @ClassName: UserUIAsync 
 * @Description: TODO(用户注册时的验证码校验、用户名格式校验) 
 * @author 詹金浩 
 * @date 2018年3月10日 下午4:18:45
 */
@WebServlet("/userUIAsync")
public class UserUIAsync extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String operation = request.getParameter("operation");
		if(operation.equals("verCode"))
			verCode(request, response);
		else if(operation.equals("isLegal")) {
			try {
				isLegal(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if (operation.equals("checkEmail")) {
			try {
				checkEmail(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @Title: checkEmail 
	 * @Description: TODO(确保邮箱的唯一性) 
	 * @param request response
	 * @throws SQLException IOException
	 */
	private void checkEmail(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		String eMail = request.getParameter("inputEmail");
		UserService userService = new UserServiceImpl();
		Boolean b = userService.isEmailExist(eMail);
		response.getWriter().write("{\"isExist\":"+b+"}");
	}

	/**
	 * @Title: isLegal 
	 * @Description: TODO(判断学号是否合法) 
	 * @param request response
	 * @throws SQLException IOException
	 */
	private void isLegal(HttpServletRequest request, HttpServletResponse response)
			throws SQLException, IOException {
		String msg = "";
		UserService userService = new UserServiceImpl();
		String inputUserId = request.getParameter("inputUserId");
		Boolean isLegal = userService.isLegalUserById(inputUserId);
		Boolean isRegister = userService.findUserById(inputUserId);
		Boolean flag = false;
		if( isLegal && !isRegister) {
			msg = "此学号可以注册！";
			flag = true;
		}
		else if(isRegister)
			msg = "学号已被注册！";
		else 
			msg = "学号格式错误！";
		String wholeMsg = "{\"legalMsg\":\""+msg+"\",\"isLegal\":"+flag+"}";
		response.getWriter().write(wholeMsg);
	}
	
	/**
	 * @Title: verCode 
	 * @Description: TODO(校验验证码是否正确) 
	 * @param request response
	 * @throws IOException
	 */
	private void verCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String inputVerCode = (String)request.getParameter("inputVerCode").toLowerCase();
		
		String verCodeFromServer = ((String) request.getSession().getAttribute("rightVerCode")).toLowerCase();
		
		//验证码正确时isError为false  不正确时为true
		Boolean b = inputVerCode.equals(verCodeFromServer)?false:true;
		
		response.getWriter().write("{\"isError\":"+b+"}");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}